Page({
    
  data: {
      goods: [{
              "name": "临期三月",
              "type": -1,
              "foods": [
                  {
                      "name": "面包",
                      "price": 3.5,
                      "originalPrice": "5",
                      "Count": 0,
                      "mounth": "十二月底",
                      "icon": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.taopic.com%2Fuploads%2Fallimg%2F120607%2F188923-12060G9213792.jpg&refer=http%3A%2F%2Fimg.taopic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637765901&t=b712efca2adc625ee510c28442e3f868",
                  },
              ]},
          {
              "name": "临期二月",
              "type": 2,
              "foods": [
                  {
                      "name": "泡面",
                      "price": 3,
                      "originalPrice": "4",
                      "Count": 0,
                      "mounth": "十一月底",
                      "icon": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F18%2F01%2F06%2F12549b06ff4ec60f28229cd420dafd59.jpg&refer=http%3A%2F%2Fku.90sjimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637765963&t=42e54cd47c794619a734eb2cef72aede",
                  }
              ]},
          {
              "name": "临期一月",
              "type": 2,
              "foods": [
                  {
                      "name": "华夫饼",
                      "price": 3,
                      "originalPrice": "5",
                      "Count": 0,
                      "mounth": "十月底",
                      "icon": "https://img1.baidu.com/it/u=1500059949,1604415508&fm=26&fmt=auto",
                  }
              ]
          },
          
      ],
      toView: '0',
      scrollTop: 100,
      foodCounts: 0,
      totalPrice: 0,// 总价格
      totalCount: 0, // 总商品数
      carArray: [],
      minPrice: 15,//起送價格
      payDesc: '',
      fold: true,
      selectFoods: [{ price: 20, count: 2 }],
      cartShow: 'none',
      status: 0,
      url :"",
      showPopup: false,

  },
  selectMenu: function (e) {
      var index = e.currentTarget.dataset.itemIndex;
      this.setData({
          toView: 'order' + index.toString()
      })
      console.log(this.data.toView);
  },
  //移除商品
  decreaseCart: function (e) {
      var index = e.currentTarget.dataset.itemIndex;
      var parentIndex = e.currentTarget.dataset.parentindex;
      this.data.goods[parentIndex].foods[index].Count--
      var name = this.data.goods[parentIndex].foods[index].name;
      var num = this.data.goods[parentIndex].foods[index].Count;
      var mark = 'a' + index + 'b' + parentIndex
      var price = this.data.goods[parentIndex].foods[index].price;
      var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
      var carArray1 = this.data.carArray.filter(item => item.mark != mark);
      carArray1.push(obj);
      console.log(carArray1);
      for (var m = 0; m < carArray1.length; m++) {
        if (carArray1[m].num == 0) {
          carArray1.splice(m, 1);  // splice(a,b); a需要删除的位置,b删除几个
        }
      }
      this.setData({
          carArray: carArray1,
          goods: this.data.goods
      })
      this.calTotalPrice()
      this.setData({
          payDesc: this.payDesc(),
      })
      //关闭弹起
      var count1 = 0
      for (let i = 0; i < carArray1.length; i++) {
          if (carArray1[i].num == 0) {
              count1++;
          }
      }
      //console.log(count1)
      if (count1 == carArray1.length) {
          if (num == 0) {
              this.setData({
                  cartShow: 'none'
              })
          }
      }
  },
  decreaseShopCart: function (e) {
      console.log('1');
      this.decreaseCart(e);
  },
  //添加到购物车
  addCart(e) {
      var index = e.currentTarget.dataset.itemIndex;
      var parentIndex = e.currentTarget.dataset.parentindex;
      this.data.goods[parentIndex].foods[index].Count++;
      var mark = 'a' + index + 'b' + parentIndex
      var price = this.data.goods[parentIndex].foods[index].price;
      var num = this.data.goods[parentIndex].foods[index].Count;
      var name = this.data.goods[parentIndex].foods[index].name;
      var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
      var carArray1 = this.data.carArray.filter(item => item.mark != mark)
      carArray1.push(obj)
      console.log(carArray1);
      this.setData({
          carArray: carArray1,
          goods: this.data.goods
      })
      this.calTotalPrice();
      this.setData({
          payDesc: this.payDesc()
      })
  },
  addShopCart: function (e) {
      this.addCart(e);
  },
  //计算总价
  calTotalPrice: function () {
      var carArray = this.data.carArray;
      var totalPrice = 0;
      var totalCount = 0;
      for (var i = 0; i < carArray.length; i++) {
          totalPrice += carArray[i].price * carArray[i].num;
          totalCount += carArray[i].num
      }
      this.setData({
          totalPrice: totalPrice,
          totalCount: totalCount,
          //payDesc: this.payDesc()
      });
  },
  //差几元起送
  payDesc() {
      if (this.data.totalPrice === 0) {
          return `￥${this.data.minPrice}元起送`;
      } else if (this.data.totalPrice < this.data.minPrice) {
          let diff = this.data.minPrice - this.data.totalPrice;
          return '还差' + diff + '元起送';
      } else {
          return '去结算';
      }
  },
  
  //购物车
  toggleList: function () {
      if (!this.data.totalCount) {
          return;
      }
      this.setData({
          fold: !this.data.fold,
      })
      var fold = this.data.fold
      //console.log(this.data.fold);
      this.cartShow(fold)
  },
  cartShow: function (fold) {
      console.log(fold);
      if (fold == false) {
          this.setData({
              cartShow: 'block',
          })
      } else {
          this.setData({
              cartShow: 'none',
          })
      }
      console.log(this.data.cartShow);
  },
  /**   
  * 预览图片  
  */
  togglePopup: function (event) {
    var image_path ="/images";      
    this.setData({
      url: image_path,
      showPopup: !this.data.showPopup
    });
  },    

  tabChange: function (e) {
      var showtype = e.target.dataset.type;
      this.setData({
          status: showtype,
      });
  },

  onLoad: function (options) {
      // 页面初始化 options为页面跳转所带来的参数
      this.setData({
          payDesc: this.payDesc()
      });
  },
  backBtn:function(){
    this.globalData.headerBtnPosi = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({ // iphonex底部适配
        success: res => {
            that.globalData.systeminfo = res
        }
    })
      wx.navigateBack()
  },
  topay: function () {
    wx.navigateTo({
      url: '../pay/pay'
    })
  },
  
})